package com.fr.cell.core;

import com.fr.base.BaseUtils;
import com.fr.base.ColumnRow;
import com.fr.base.Inter;
import com.fr.base.core.list.IntList;
import com.fr.cell.Grid;
import com.fr.cell.GridColumn;
import com.fr.cell.GridSelection;
import com.fr.cell.ReportPane;
import com.fr.report.CellElement;
import com.fr.report.Report;
import com.fr.report.TemplateReport;
import com.fr.report.core.DynamicValueList;
import com.fr.report.core.ReportHelper;
import com.fr.util.Utils;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import javax.swing.JPopupMenu;
import javax.swing.JScrollBar;
import javax.swing.JToolTip;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;

public class GridColumnMouseHandler extends MouseInputAdapter
{
  private GridColumn gridColumn;
  private int dragType = GridUtils.DRAG_NONE;
  private boolean isMulitColumnsSelectDragPermited = false;
  private int startMultiSelectIndex = 0;
  private int endMultiSelectIndex = 0;
  private boolean isDragPermited = false;
  private int dragColumnIndex = 0;
  private JToolTip columnTip = null;
  private JWindow tipWindow = null;

  public GridColumnMouseHandler(GridColumn paramGridColumn)
  {
    this.gridColumn = paramGridColumn;
  }

  public void setStartMultiSelectIndex(int paramInt)
  {
    this.startMultiSelectIndex = paramInt;
  }

  public void setEndMultiSelectIndex(int paramInt)
  {
    this.endMultiSelectIndex = paramInt;
  }

  public void mousePressed(MouseEvent paramMouseEvent)
  {
    GridSelection localGridSelection;
    Object localObject;
    if (!(this.gridColumn.isEnabled()))
      return;
    ReportPane localReportPane = this.gridColumn.getReportPane();
    TemplateReport localTemplateReport = localReportPane.getEditingReport();
    localReportPane.getGrid().stopEditing();
    int i = paramMouseEvent.getX();
    int j = paramMouseEvent.getY();
    boolean bool1 = paramMouseEvent.isShiftDown();
    boolean bool2 = paramMouseEvent.isControlDown();
    boolean bool3 = SwingUtilities.isRightMouseButton(paramMouseEvent);
    ColumnRow localColumnRow = GridUtils.getAdjustEventColumnRow(localReportPane, i, j);
    if (!(localReportPane.isColumnEndless()))
    {
      int k = localTemplateReport.getColumnCount() - 1;
      if (localColumnRow.getColumn() > k + 1)
        return;
    }
    DynamicValueList localDynamicValueList = ReportHelper.getColumnWidthList(localTemplateReport);
    int l = localReportPane.getGrid().getHorizontalValue();
    int i1 = localReportPane.getGrid().getHorizontalExtent();
    int i2 = l + i1 + 1;
    int i3 = localReportPane.getGrid().getHorizontalBeginValue();
    double d1 = 0.0D;
    double d3 = 0.0D;
    for (int i4 = i3; i4 <= i2; ++i4)
    {
      double d2;
      if (i4 == 0)
        i4 = l;
      d1 += d3;
      d3 = localDynamicValueList.get(i4);
      if (d3 <= 0.0D)
        d2 = d1 + 1.0D;
      else
        d2 = d1 + d3;
      if ((isOnSeparatorLineIncludeZero(i, d2, d3)) || (isOnNormalSeparatorLine(i, d2)))
      {
        this.dragType = GridUtils.DRAG_WIDTH;
        this.isDragPermited = true;
        this.dragColumnIndex = i4;
        showToolTip(paramMouseEvent, createToolTipString(localDynamicValueList.get(this.dragColumnIndex), localDynamicValueList.getRangeValue(0, this.dragColumnIndex + 1)));
        break;
      }
      if ((i > d1) && (i <= d2))
      {
        this.dragType = GridUtils.DRAG_SELECT_COLUMNS;
        this.isMulitColumnsSelectDragPermited = true;
        this.startMultiSelectIndex = i4;
        showToolTip(paramMouseEvent, createToolTipString(1));
        break;
      }
    }
    if (bool3)
    {
      localGridSelection = localReportPane.getGridSelection();
      localGridSelection.setType(0);
      localObject = GridUtils.getGridSelectionColumnIndex(localGridSelection);
      if ((localColumnRow.getColumn() < ((IntList)localObject).get(0)) || (localColumnRow.getColumn() > ((IntList)localObject).get(((IntList)localObject).size() - 1)))
        doSelectColumn(localColumnRow.getColumn());
      JPopupMenu localJPopupMenu = localReportPane.createColumnPopupMenu(paramMouseEvent, Math.max(this.dragColumnIndex, Math.min(this.startMultiSelectIndex, this.endMultiSelectIndex)));
      if (localJPopupMenu != null)
        GUICoreUtils.showPopupMenu(localJPopupMenu, this.gridColumn, i + 1, j + 1);
    }
    else if (this.dragType == GridUtils.DRAG_SELECT_COLUMNS)
    {
      localGridSelection = localReportPane.getGridSelection();
      localGridSelection.setType(0);
      if (bool1)
      {
        localObject = null;
        if (bool2)
          localObject = localGridSelection.getLastCellRectangle();
        else
          localObject = localGridSelection.getFirstCellRectangle();
        doShiftSelectColumn(localReportPane, i, j, (Rectangle)localObject);
      }
      else if (bool2)
      {
        doControlSelectColumn(localReportPane, i, j);
      }
      else
      {
        localColumnRow = GridUtils.getEventColumnRow(localReportPane, i, j);
        doSelectColumn(localColumnRow.getColumn());
      }
    }
    this.gridColumn.getReportPane().repaint();
  }

  private void doShiftSelectColumn(ReportPane paramReportPane, double paramDouble1, double paramDouble2, Rectangle paramRectangle)
  {
    ColumnRow localColumnRow = GridUtils.getAdjustEventColumnRow(paramReportPane, paramDouble1, paramDouble2);
    int i = localColumnRow.getColumn();
    int j = localColumnRow.getRow();
    GridSelection localGridSelection = paramReportPane.getGridSelection();
    Rectangle localRectangle1 = localGridSelection.getEditRectangle();
    int k = localRectangle1.x;
    if (localColumnRow.getColumn() >= localRectangle1.x)
      localColumnRow = ColumnRow.valueOf(localColumnRow.getColumn() + 1, localColumnRow.getRow());
    else
      ++k;
    int l = GridUtils.getAdjustLastColumnRowOfReportPane(paramReportPane).getRow();
    Rectangle localRectangle2 = new Rectangle(Math.min(k, localColumnRow.getColumn()), 0, Math.max(localRectangle1.width, Math.abs(k - localColumnRow.getColumn())), l);
    paramRectangle.setBounds(localRectangle2.x, localRectangle2.y, localRectangle2.width, localRectangle2.height);
    paramReportPane.ensureColumnRowVisible(i, j);
  }

  private void doControlSelectColumn(ReportPane paramReportPane, double paramDouble1, double paramDouble2)
  {
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    GridSelection localGridSelection = paramReportPane.getGridSelection();
    localGridSelection.setType(0);
    int i = GridUtils.getAdjustLastColumnRowOfReportPane(paramReportPane).getRow();
    ColumnRow localColumnRow = GridUtils.getAdjustEventColumnRow(paramReportPane, paramDouble1, paramDouble2);
    CellElement localCellElement = localTemplateReport.getCellElement(localColumnRow.getColumn(), 0);
    if (localCellElement == null)
    {
      localGridSelection.setEditBounds(localColumnRow.getColumn(), 0, 1, 1);
      localGridSelection.addCellRectangle(new Rectangle(localColumnRow.getColumn(), 0, 1, i));
    }
    else
    {
      localGridSelection.setEditBounds(localCellElement.getColumn(), localCellElement.getRow(), 1, localCellElement.getRowSpan());
      localGridSelection.addCellRectangle(new Rectangle(localCellElement.getColumn(), 0, 1, i));
    }
    paramReportPane.setGridSelection(localGridSelection);
    dealCellSelection(paramReportPane);
    paramReportPane.ensureColumnRowVisible(localColumnRow.getColumn(), localColumnRow.getRow());
  }

  public void mouseReleased(MouseEvent paramMouseEvent)
  {
    if (!(this.gridColumn.isEnabled()))
      return;
    ReportPane localReportPane = this.gridColumn.getReportPane();
    localReportPane.getGrid().stopEditing();
    this.isMulitColumnsSelectDragPermited = false;
    this.isDragPermited = false;
    hideToolTip();
    if (this.dragType == GridUtils.DRAG_WIDTH)
    {
      IntList localIntList;
      DynamicValueList localDynamicValueList;
      int i2;
      int i3;
      GridSelection localGridSelection = localReportPane.getGridSelection();
      int i = localGridSelection.getType();
      if (i == 0)
      {
        int j = 1;
        int k = GridUtils.getAdjustLastColumnRowOfReportPane(localReportPane).getRow();
        for (int l = 0; l < localGridSelection.getCellRectangleCount(); ++l)
        {
          int i1 = localGridSelection.getCellRectangle(l).height;
          if (i1 < k)
          {
            j = 0;
            break;
          }
        }
        if (j != 0)
        {
          localIntList = GridUtils.getGridSelectionColumnIndex(localGridSelection);
          localDynamicValueList = ReportHelper.getColumnWidthList(localReportPane.getEditingReport());
          i2 = localDynamicValueList.get(this.dragColumnIndex);
          for (i3 = 0; i3 < localIntList.size(); ++i3)
          {
            int i4;
            if (localIntList.get(i3) == this.dragColumnIndex)
              for (i4 = 0; i4 < localIntList.size(); ++i4)
                localDynamicValueList.set(localIntList.get(i4), i2);
          }
        }
      }
      localReportPane.getVerticalScrollBar().setValue(localReportPane.getVerticalScrollBar().getValue());
      localReportPane.getHorizontalScrollBar().setValue(localReportPane.getHorizontalScrollBar().getValue());
    }
    this.dragType = GridUtils.DRAG_NONE;
    this.dragColumnIndex = 0;
    localReportPane.fireReportDataChanged();
    localReportPane.undoRecord();
  }

  public void mouseDragged(MouseEvent paramMouseEvent)
  {
    int i3;
    int i4;
    if (!(this.gridColumn.isEnabled()))
      return;
    ReportPane localReportPane = this.gridColumn.getReportPane();
    TemplateReport localTemplateReport = localReportPane.getEditingReport();
    if ((this.dragType == GridUtils.DRAG_NONE) || (SwingUtilities.isRightMouseButton(paramMouseEvent)))
      return;
    GridSelection localGridSelection = localReportPane.getGridSelection();
    localReportPane.getGrid().stopEditing();
    int i = paramMouseEvent.getX();
    int j = paramMouseEvent.getY();
    if (i < 0)
      i = 0;
    if (this.dragType == GridUtils.DRAG_SELECT_COLUMNS)
    {
      if (!(this.isMulitColumnsSelectDragPermited))
        return;
      int k = localTemplateReport.getColumnCount() - 1;
      ColumnRow localColumnRow = GridUtils.getAdjustEventColumnRow(localReportPane, i, j);
      if ((!(localReportPane.isColumnEndless())) && (localColumnRow.getColumn() > k + 1))
        return;
      this.endMultiSelectIndex = localColumnRow.getColumn();
      Rectangle localRectangle1 = null;
      Rectangle localRectangle2 = null;
      i3 = GridUtils.getAdjustLastColumnRowOfReportPane(localReportPane).getRow();
      if (this.endMultiSelectIndex > this.startMultiSelectIndex)
      {
        i4 = this.endMultiSelectIndex - this.startMultiSelectIndex + 1;
        localRectangle1 = new Rectangle(this.startMultiSelectIndex, 0, 1, 1);
        localRectangle2 = new Rectangle(this.startMultiSelectIndex, 0, i4, i3);
      }
      else if (this.endMultiSelectIndex < this.startMultiSelectIndex)
      {
        i4 = this.startMultiSelectIndex - this.endMultiSelectIndex + 1;
        localRectangle1 = new Rectangle(this.startMultiSelectIndex, 0, 1, 1);
        localRectangle2 = new Rectangle(this.endMultiSelectIndex, 0, i4, i3);
      }
      else
      {
        localRectangle1 = new Rectangle(this.startMultiSelectIndex, 0, 1, 1);
        localRectangle2 = new Rectangle(this.startMultiSelectIndex, 0, 1, i3);
      }
      localGridSelection.setType(0);
      localGridSelection.setEditBounds(localRectangle1.x, localRectangle1.y, localRectangle1.width, localRectangle1.height);
      localGridSelection.setOnlyCellBounds(localRectangle2.x, localRectangle2.y, localRectangle2.width, localRectangle2.height);
      dealCellSelection(localReportPane);
      localReportPane.fireGridSelectionChanged();
      label630: setToolTipText2(createToolTipString(Math.abs(this.startMultiSelectIndex - this.endMultiSelectIndex) + 1));
    }
    else if (this.dragType == GridUtils.DRAG_WIDTH)
    {
      if (!(this.isDragPermited))
        return;
      DynamicValueList localDynamicValueList = ReportHelper.getColumnWidthList(localTemplateReport);
      int l = localReportPane.getGrid().getHorizontalValue();
      int i1 = localReportPane.getGrid().getHorizontalExtent();
      int i2 = l + i1 + 1;
      i3 = localReportPane.getGrid().getHorizontalBeginValue();
      i4 = 0;
      int i5 = 0;
      int i7 = 0;
      for (int i9 = i3; i9 <= i2; ++i9)
      {
        int i6;
        if (i9 == 0)
          i9 = l;
        i5 += i7;
        int i8 = localDynamicValueList.get(i9);
        if (i8 <= 0)
          i6 = i5 + 1;
        else
          i6 = i5 + i8;
        if (i9 == this.dragColumnIndex)
          i4 = i5;
        if ((i >= i5) && (i < i6))
        {
          if (i9 >= this.dragColumnIndex)
          {
            localDynamicValueList.set(this.dragColumnIndex, i - i4);
            break;
          }
          localDynamicValueList.set(i9, i - i5);
          int i10 = this.dragColumnIndex - 1;
          while (true)
          {
            if (i10 <= i9)
              break label630;
            localDynamicValueList.set(i10, 0);
            --i10;
          }
        }
      }
      setToolTipText2(createToolTipString(localDynamicValueList.get(this.dragColumnIndex), localDynamicValueList.getRangeValue(0, this.dragColumnIndex + 1)));
    }
    this.gridColumn.getReportPane().repaint();
  }

  public void mouseMoved(MouseEvent paramMouseEvent)
  {
    if (!(this.gridColumn.isEnabled()))
      return;
    ReportPane localReportPane = this.gridColumn.getReportPane();
    TemplateReport localTemplateReport = localReportPane.getEditingReport();
    this.gridColumn.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(BaseUtils.readImage("/com/fr/cell/images/cursor/cursor_hselect.gif"), new Point(16, 16), "hor_select"));
    int i = paramMouseEvent.getX();
    DynamicValueList localDynamicValueList = ReportHelper.getColumnWidthList(localTemplateReport);
    int j = localReportPane.getGrid().getHorizontalValue();
    int k = localReportPane.getGrid().getHorizontalExtent();
    int l = j + k + 1;
    int i1 = localReportPane.getGrid().getHorizontalBeginValue();
    double d1 = 0.0D;
    double d3 = 0.0D;
    for (int i2 = i1; i2 < l; ++i2)
    {
      double d2;
      if (i2 == 0)
        i2 = j;
      d1 += d3;
      d3 = localDynamicValueList.get(i2);
      if (d3 <= 0.0D)
        d2 = d1 + 1.0D;
      else
        d2 = d1 + d3;
      if (isOnSeparatorLineIncludeZero(i, d2, d3))
      {
        this.gridColumn.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(BaseUtils.readImage("/com/fr/cell/images/cursor/cursor_hsplit.gif"), new Point(16, 16), "ver_split"));
        return;
      }
      if (isOnNormalSeparatorLine(i, d2))
      {
        this.gridColumn.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(BaseUtils.readImage("/com/fr/cell/images/cursor/cursor_hmove.gif"), new Point(16, 16), "ver_move"));
        return;
      }
    }
  }

  private boolean isOnSeparatorLineIncludeZero(double paramDouble1, double paramDouble2, double paramDouble3)
  {
    return ((paramDouble3 == 0.0D) && (paramDouble1 >= paramDouble2 + 2.0D) && (paramDouble1 <= paramDouble2 + 5.0D));
  }

  private boolean isOnNormalSeparatorLine(double paramDouble1, double paramDouble2)
  {
    return ((paramDouble1 >= paramDouble2 - 2.0D) && (paramDouble1 <= paramDouble2 + 2.0D));
  }

  private void showToolTip(MouseEvent paramMouseEvent, String paramString)
  {
    if (this.tipWindow == null)
    {
      this.tipWindow = new JWindow();
      this.columnTip = this.gridColumn.createToolTip();
      this.tipWindow.getContentPane().add(this.columnTip, "Center");
    }
    this.columnTip.setTipText(paramString);
    Point localPoint = new Point(paramMouseEvent.getX(), 0);
    SwingUtilities.convertPointToScreen(localPoint, this.gridColumn);
    Dimension localDimension1 = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension localDimension2 = this.columnTip.getPreferredSize();
    int i = Math.max(0, Math.min(localPoint.x - localDimension2.width / 2, localDimension1.width - localDimension2.width));
    int j = localPoint.y - localDimension2.height - 2;
    this.tipWindow.setLocation(i, j);
    this.tipWindow.pack();
    this.tipWindow.setVisible(true);
  }

  private void hideToolTip()
  {
    if (this.tipWindow != null)
      this.tipWindow.setVisible(false);
  }

  private void setToolTipText2(String paramString)
  {
    if (this.columnTip == null)
      return;
    this.columnTip.setTipText(paramString);
    this.columnTip.setSize(this.columnTip.getPreferredSize());
    this.tipWindow.pack();
    this.tipWindow.repaint();
  }

  private String createToolTipString(double paramDouble1, double paramDouble2)
  {
    return Inter.getLocText("Width") + ": " + Utils.doubleToString(paramDouble1) + "/" + Utils.doubleToString(paramDouble2) + Inter.getLocText("px");
  }

  private String createToolTipString(int paramInt)
  {
    return paramInt + "C";
  }

  private void doSelectColumn(int paramInt)
  {
    ReportPane localReportPane = this.gridColumn.getReportPane();
    GridSelection localGridSelection = localReportPane.getGridSelection();
    localGridSelection.setType(0);
    this.startMultiSelectIndex = (this.endMultiSelectIndex = paramInt);
    int i = GridUtils.getAdjustLastColumnRowOfReportPane(localReportPane).getRow();
    localGridSelection.setEditBounds(paramInt, 0, 1, 1);
    localGridSelection.setOnlyCellBounds(paramInt, 0, 1, i);
    dealCellSelection(localReportPane);
    localReportPane.fireGridSelectionChanged();
  }

  private void dealCellSelection(ReportPane paramReportPane)
  {
    GridSelection localGridSelection = paramReportPane.getGridSelection();
    int i = localGridSelection.getType();
    if (i == 1)
      return;
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    Rectangle localRectangle = localGridSelection.getEditRectangle();
    int j = localRectangle.x;
    int k = localRectangle.y;
    int l = localRectangle.width;
    int i1 = localRectangle.height;
    for (CellElement localCellElement = localTemplateReport.getCellElement(j, k); localCellElement != null; localCellElement = localTemplateReport.getCellElement(j, k))
    {
      if ((localCellElement.getRowSpan() == 1) && (localCellElement.getColumnSpan() == 1))
        break;
      k += localCellElement.getRowSpan();
    }
    localRectangle.setBounds(j, k, l, i1);
  }
}